package com.bjwl.web.contrlller.admin;

import com.bjwl.pojo.BranchStore;
import com.bjwl.pojo.Bsadmin;
import com.bjwl.pojo.vo.BsadminPowerVos;
import com.bjwl.service.BsadminPowerService;
import com.bjwl.service.BsadminService;
import com.bjwl.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author: dzx
 * @Date:Create：in 2020/4/14 20:43
 * @Description:
 * @Modified By：
 * @Version: 1.0
 */

@Controller
@RequestMapping("/bsAdmin")
public class BsAdminController {

    @Autowired
    private BsadminService bsadminService;

    @Autowired
    private BsadminPowerService bsadminPowerService;

    @RequestMapping("/findBsadminPowerById")
    public String findBsadminPowerById(Integer baid, Model model) {
        model.addAttribute("BsadminPower", bsadminPowerService.findBsadminPowerById(baid));
        model.addAttribute("powers", bsadminPowerService.findAllPower());
        return "main_oa/role";
    }


    @RequestMapping("/updatedBsadminPowerById")
    public @ResponseBody
    boolean updatedBsadminPowerById(@RequestBody BsadminPowerVos vos) {

        bsadminPowerService.deleteBsadminPowerById(vos.getId());
        bsadminPowerService.saveBsadminPowerById(vos);

        return true;
    }

    /**
     * 根据分店id查询分店管理员  分页
     */
    @RequestMapping("/findBsAdmminById")
    public String findBsAdmminById(String username, Integer bsId, Integer pageNum, Model model, HttpSession httpSession) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (bsId == null) {
            bsId = (Integer) httpSession.getAttribute("bsId");
        } else {
            httpSession.setAttribute("bsId", bsId);
        }
        PageBean<Bsadmin> pageBean = bsadminService.findBsAdminsByBranchStore(pageNum, bsId, username);
        model.addAttribute("pageBean", pageBean);
        return "main_oa/branch-list";
    }


    /**
     * 根据分店id添加分店管理员
     *
     * @param bsadmin
     * @return
     */
    @RequestMapping("/addBsAdmin")
    public @ResponseBody
    Boolean addBsAdmin(Bsadmin bsadmin, HttpSession httpSession) {
        bsadmin.setBsid((Integer) httpSession.getAttribute("bsId"));
        List<Bsadmin> bsadmins = bsadminService.findBsadminByName(bsadmin.getUsername());
        if (bsadmins.size() == 0) {
            try {
                bsadminService.addBsAdmin(bsadmin);
                // new Date()为获取当前系统时间
                bsadmin.setInserttime(new Date());
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
        BsadminPowerVos vos = new BsadminPowerVos();
        vos.setId(bsadmin.getId());
        if(bsadmin.getJl() == 1){
            vos.getPowerid().add(1);
            vos.getPowerid().add(2);
            vos.getPowerid().add(3);
            vos.getPowerid().add(7);
        }else{
            vos.getPowerid().add(1);
            vos.getPowerid().add(2);
            vos.getPowerid().add(3);
        }

        bsadminPowerService.saveBsadminPowerById(vos);

        return true;
    }

    /**
     * 根据id查询分店管理员并回显
     */
    @RequestMapping("/findBsadminById")
    public String findBsadminById(Bsadmin bsadmin, Model model) {
        Bsadmin bsadmin1 = bsadminService.findBsadminById(bsadmin);
        model.addAttribute("bsadmin", bsadmin1);
        return "main_oa/branch-edit";
    }

    /**
     * 根据id修改分店管理员信息
     */
    @RequestMapping("/updateBsadminById")
    public @ResponseBody
    Boolean updateBsadminById(Bsadmin bsadmin) {
        try {
            bsadminService.updateBsadminById(bsadmin);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 根据id删除分店管理员
     */
    @RequestMapping("/deleteBsadminById")
    public @ResponseBody
    Boolean deleteBsadminById(Bsadmin bsadmin) {
        try {
            bsadminService.deleteBsadminById(bsadmin);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 启用停用分店管理员
     */
    @RequestMapping("/stateBsAdmin")
    public @ResponseBody
    Boolean stateBsAdmin(Bsadmin bsadmin) {
        bsadminService.stateBsAdmin(bsadmin);
        return true;
    }


}
